// packageA/pages/org-appointment/index.js
import valid from '../../utils/valid'
const dayjs = require("dayjs")
const WXAPI = require('../../api/org')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    minDate: new Date().getTime(),
    maxDate: new Date(2040, 10, 1).getTime(),
    currentDate: new Date().getTime(),
    isShowDate:false,
    dVisitTime:'',
    iVisitorsCount:'',
    sContactName:'',
    sContactTel:'',
    orgInfo:{},
    phoneValuate: true,
  },
  cancel() {
    this.setData({
      isShowDate: false
    })
  },
  confirm(event) {
    this.setData({
      currentDate: event.detail,
      dVisitTime: dayjs(event.detail).format("YYYY-MM-DD HH:mm:ss")
    });
    this.setData({
      isShowDate: false
    })
  },
  selectDateTime(){
    this.setData({
      isShowDate: true
    })
  },
  async saveAppoint() {
    this.setData({
      isSubmit: true
    })
    // 手动校验必填项
    if(this.data.dVisitTime && this.data.iVisitorsCount && this.data.sContactName && this.data.sContactTel){
      const { dVisitTime,iVisitorsCount,sContactName,sContactTel,sReason } = this.data
      let data = {
        sOrgId: this.data.orgInfo.orgId,
        dVisitTime,
        iVisitorsCount,
        sContactName,
        sContactTel,
        sReason
      }
      let res = await WXAPI.submitAppointOrg(data)
      if(res.data.code == 0) {
        wx.showToast({
          title: res.data.message
        })
        setTimeout(() => {
          // wx.navigateBack({
          //   delta: 1
          // })
          wx.redirectTo({
            url: '/packageA/pages/org-appoint-records/index',
          })
        }, 2000);
      }
    }
    
  },
  phoneNumberValid(){
    this.setData({
      phoneValuate: valid.validatePhoneOrNum(this.data.sContactTel) 
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.orgObj) {
      this.setData({
        orgInfo: JSON.parse(options.orgObj)
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})